import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Solution {

    static class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
      TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
  }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return plan(p).equals(plan(q));
    }
    public List<Integer> plan(TreeNode root){
        List<Integer> list = new ArrayList<>();
        TreeNode tmp = null;
        while(root!=null){
            if(root.left!=null){
                tmp = root.left;
                while(tmp.right!=null&&tmp.right!=root){
                    tmp = tmp.right;
                }
                if(tmp.right == null){
                    list.add(0);
                    tmp.right = root;
                    root = root.left;
                }
                else{
                    list.add(root.val);
                    tmp.right = null;
                    root = root.right;
                }
            }else{
                list.add(root.val);
                root = root.right;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);

        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node5.right = node6;

        Solution s = new Solution();
        List l = s.plan(node1);


    }

}

class Solution22 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        if (root.left == null && root.right == null) return true;

        return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) &&
                isBalanced(root.right);
    }
    public int height(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null) return 1;

        return Math.max(height(root.left),height(root.right)) + 1;
    }
}




public class Main {

    static class TreeNode{
        TreeNode left;
        TreeNode right;
        char val;

        public TreeNode(char val){
            this.val = val;
        }
    }


    static int i = 0;
    public static TreeNode createTree_By_preOrder(String str){

        TreeNode root = null;

        if(str.charAt(i) != '#'){
            root = new TreeNode(str.charAt(i++));

            root.left = createTree_By_preOrder(str);

            root.right = createTree_By_preOrder(str);
        }else{
            i++;
        }
        return root;
    }
    public static void inOrder(TreeNode root){
        if(root == null) return;
        if(root.left == null && root.right == null){
            System.out.println(root.val);
        }

        inOrder(root.left);
        System.out.println(root.val + " ");
        inOrder(root.right);
    }


    public static void main(String[] args) {
        //读入字符串
        Scanner in = new Scanner(System.in);

        while(in.hasNextLine()){
            String str = in.nextLine();

            TreeNode root = createTree_By_preOrder(str);

            inOrder(root);
        }
    }
}

class Solution222 {

    static class TreeNode{
        TreeNode left;
        TreeNode right;
        char val;

        public TreeNode(char val){
            this.val = val;
        }
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(p == root || q == root) return root;

        if (find(root.right,p) && find(root.left,q)) return root;
        if(!find(root.right,p) && !find(root.left,q)) return root;

        if(find(root.left,p) && find(root.left,q))
            return lowestCommonAncestor(root.left,p,q);
        if(find(root.right,p) && find(root.right,q))
            return lowestCommonAncestor(root.right,p,q);
        return null;
    }

    //查看p或q位于的位置
    public boolean find(TreeNode root ,TreeNode t){

        if(root == null) return false;

        if(t == null) return true;

        return find(root.left,t) || find(root.right,t) || root.val == t.val;
    }
}

